import {
  getBase64,
  getLocalTemplateUrl,
} from "./index.js";
import {renderAsync} from "docx-preview";

class Commands {

  constructor(editor) {
    this.editor = editor
  }
  /**
   * 获取commands
   * @return SingleCommands
   */
  getCommands () {
    return this.editor.commands
  }

  // 清除格式
  clearFormat () {
    this.getCommands().resetAttributes('paragraph', ['style', 'class'])
    this.getCommands().unsetColor()
    this.getCommands().unsetTextAlign()
    this.getCommands().unsetStrike()
    this.getCommands().unsetFontSize()
    this.getCommands().unsetFontFamily()
    this.getCommands().unsetIndent()
    this.getCommands().unsetFirstIndent()
    this.getCommands().unsetAllMarks()
    this.getCommands().focus()
  }
// 加粗
  toggleBold () {
    this.getCommands().toggleMark('bold')
    this.getCommands().focus()
  }
// 倾斜
  toggleItalic () {
    this.getCommands().toggleMark('italic')
    this.getCommands().focus()
  }

  /**
   * 下划线
   */
  toggleUnderline () {
    this.getCommands().toggleUnderline()
    this.getCommands().focus()
  }

  async handleFileChange (event, cb) {
    const files = [...event.target.files]
    for (const file of files) {
      this.getCommands().setImage({ src: cb ? await cb(file) : await getLocalTemplateUrl(await getBase64(file)) })
      this.getCommands().setTextAlign('center')
    }
    this.getCommands().focus()
  }
// 插入图片
  insertOrReplaceImage (cb) {
    const upload = document.createElement('input')
    upload.type = 'file'
    upload.accept = 'image/*'
    upload.multiple = true
    upload.click()
    upload.onchange =  (ev) => this.handleFileChange(ev, cb)
    // this.getCommands().setImage({ src: `https://picsum.photos/${Math.floor(Math.random()* 100 + 100)}/${Math.floor(Math.random()* 100 + 100)}`, alt: UUID() })
  }
  // 设置字体颜色
  selectFontColor (color) {
    this.getCommands().setColor(color)
    this.getCommands().focus()
  }
  // 设置字体颜色
  selectBackColor (color) {
    this.getCommands().setHighlight({color})
    this.getCommands().focus()
  }

  // 设置字体大小
  selectFontSize (fontsize) {
    this.getCommands().setFontSize(fontsize)
    this.getCommands().focus()
    return fontsize
  }
  /**
   * 设置段前缩进
   * @param {'up' | 'down'} indent
   */
  handleIndent (indent = 'up') {
    if (indent === 'up') {
      this.getCommands().setUpIndent()
    } else {
      this.getCommands().setDownIndent()
    }
    this.getCommands().focus()
  }
// 设置首行缩进
  handleTextIndent () {
    this.getCommands().setFirstIndent('in')
    this.getCommands().focus()
  }
// 插入表格
  handleInsertTable (rows, cols) {
    this.getCommands().insertTable({ rows, cols })
    this.getCommands().focus()
  }
// 撤回
  handleUndo () {
    this.getCommands().undo()
    this.getCommands().focus()
  }
// 重做
  handleRedo () {
    this.getCommands().redo()
    this.getCommands().focus()
  }
  /**
   * 设置对齐方式
   * @param alignType
   */
  handleAlign (alignType) {
    this.getCommands().setTextAlign(alignType)
    this.getCommands().focus()
  }
  // 设置表格对齐方式
  handleTableAlign (alignType) {
    this.getCommands().setTableAlign(alignType)
    this.getCommands().focus()
  }

// 导入word内容
  async handleImportWordChange2 () {
    const input = document.createElement('input')
    input.type = 'file'
    input.accept = '.docx'
    input.click()
    input.onchange = (e) => {
      const files = [...e.target.files]
      const file = files[0]
      const tempDOM = document.createElement('div')
      renderAsync(file, tempDOM, void 0, {
        breakPages: false
      }).then((x) => {
        setTimeout(() => {
          this.getCommands().setContent(tempDOM.querySelector('article').innerHTML)
        }, 1000)
      })
    }
  }
  /**
   * 设置链接
   */
  setLink () {
    const previousUrl = this.editor.getAttributes('link').href
    const url = window.prompt('URL', previousUrl)

    // cancelled
    if (url === null) {
      return
    }

    // empty
    if (url === '') {
      this.editor
          .chain()
          .focus()
          .extendMarkRange('link')
          .unsetLink()
          .run()

      return
    }

    // update link
    this.editor
        .chain()
        .focus()
        .extendMarkRange('link')
        .setLink({ href: url })
        .run()
  }
}

export default Commands